<template>
    <div class="app-container">
        <div class="head">
            <img src="@/assets/img/Layout/position.png" />
            <p class="see-block">退料单</p>
        </div>
        <div class="view-container">
            <el-form :model="queryParams" ref="queryForm" :inline="true">
                <el-form-item label="按业务日期" prop="time">
                    <el-date-picker
                        v-model="time"
                        type="daterange"
                        range-separator="至"
                        style="width: 230px;"
                        @change="dataTime()"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="退料人" prop="returnerName">
                    <el-input
                        v-model="queryParams.returnerName"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="单据号" prop="code">
                    <el-input
                        v-model="queryParams.code"
                        clearable
                        size="small"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
                </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button
                        class="btn-success"
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="add()"
                        v-hasPermi="['system:materialReturn:add']"
                    >新建</el-button>
                </el-col>
            </el-row>
            <el-table v-loading="loading" :data="list" @row-click="handleView" border>
                <el-table-column label="序号" type="index" align="center" width="50">
                    <template slot-scope="scope">
                        <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="单据编号"  align="center" prop="code" width="150" />
                <el-table-column label="业务日期" align="center" prop="businessDate" width="180" />
                <el-table-column label="物料名称" align="center" prop="productName" width="150">
                    <template slot-scope="scope">
                        <div v-for="(item, index) in scope.row.detailList" :key="index">
                            {{ item.productName }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="物料规格" align="center" prop="productSpace" width="120">
                    <template slot-scope="scope">
                        <div v-for="(item, index) in scope.row.detailList" :key="index">
                            {{ item.productSpace }}
                        </div>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="批次号" align="center" prop="batchNumber" width="180">
                    <template slot-scope="scope">
                        <div v-for="(item, index) in scope.row.detailList" :key="index">
                            {{ item.batchNumber }}
                        </div>
                    </template>
                </el-table-column> -->
                <el-table-column label="数量" align="center" prop="realNum">
                    <template slot-scope="scope">
                        <div v-for="(item, index) in scope.row.detailList" :key="index">
                            {{ item.realNum }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="单位" align="center" prop="unit">
                    <template slot-scope="scope">
                        <div v-for="(item, index) in scope.row.detailList" :key="index">
                            {{ item.unit }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="仓库" align="center" prop="receivingWarehouse" />
                <el-table-column label="退料人" align="center" prop="returnerName" />
                <el-table-column label="审核人" align="center" prop="approveName" />
                <el-table-column label="状态" align="center" prop="status">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status == '0'"></div>
                        <div v-if="scope.row.status == '1'" style="color:#13C2C2">待审核</div>
                        <div v-if="scope.row.status == '2'" style="color:#13C2C2">待退料</div>
                        <div v-if="scope.row.status == '3'" style="color:#52C41A">已退料</div>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button
                            v-if="scope.row.approveStatus !== '1'"
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click.native.stop="handleDelete(scope.row)"
                            v-hasPermi="['system:materialReturn:remove']"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </div>
    </div>
</template>

<script>
import { materialReturnList, materialReturnDelete } from "@/api/system/warehouse/materialReturn"
import { queryWarehouse } from "@/api/system/warehouse/warehouse"
export default {
    name: "list",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 总条数
            total: 0,
            // 列表数据
            list: [],
            time: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                startTime: '',
                endTime: '',
                returnerName: '', // 退料人
                code: '', // 单据号
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        // 获取列表
        getList() {
            this.loading = true
            materialReturnList(this.addDateRange(this.queryParams)).then(res => {
                this.list = res.rows
                this.total = res.total
                this.loading = false
                this.getWarehouseList()
            })
        },
        // 获取仓库列表
        getWarehouseList(){
            queryWarehouse().then(res => {
                if(res.code == 200){
                    this.list.forEach(i => {
                        let dataList = res.data.find(item => {
                            return item.warehouseId == i.receivingWarehouse
                        })
                        if(dataList != undefined){
                            i.receivingWarehouse = dataList.name
                        }
                    })
                }
            })
        },
        // 时间选择器
        dataTime(){
            if(this.time){
                let day = 24 * 60 * 60 * 1000
                this.queryParams.startTime = this.parseTime(this.time[0])
                this.queryParams.endTime = this.parseTime(this.time[1].getTime() + day)
            }else if(this.time == null){
                this.queryParams.startTime = ''
                this.queryParams.endTime = ''
            }
        },
        // 搜索按钮
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        // 重置按钮
        resetQuery() {
            this.resetForm("queryForm")
            this.time = ''
            this.queryParams.startTime = ''
            this.queryParams.endTime = ''
            this.handleQuery()
        },
        // 新建
        add(){
            this.$router.push({path:"/material/materialReturn/add"})
        },
        // 查看
        handleView(row) {
            this.$router.push({path:"/material/materialReturn/detail",query:{id: row.id}})
        },
        // 删除
        handleDelete(row){
            this.$modal.confirm('是否确认删除当前数据？', '删除').then(() => {
                materialReturnDelete(row.id).then(res => {
                    if(res.code == 200){
                        this.$modal.msgSuccess("删除成功")
                        this.getList()
                    }
                })
            }).catch(() => {})
        }
    }
}
</script>